package com.ryan.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ryan.order.entity.Order;
import com.ryan.order.feignclient.MemberClient;
import com.ryan.order.feignclient.RouteClient;
import com.ryan.order.mapper.OrderMapper;
import com.ryan.order.service.OrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ryan.order.utils.OrderNoUtil;
import com.ryan.utils.vo.OrderMember;
import com.ryan.utils.vo.OrderRoute;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author ryan
 * @since 2020-07-31
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Autowired
    private RouteClient routeClient;

    @Autowired
    private MemberClient memberClient;

    /**
     * 条件+分页查询所有的订单
     * @param pageIndex
     * @param pageSize
     * @param orderNo
     * @return
     */
    @Override
    public Page<Order> getOrderList(long pageIndex, long pageSize, String orderNo) {

        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");
        if(!StringUtils.isEmpty(orderNo)){
            wrapper.eq("order_no", orderNo);
        }
        Page<Order> page = new Page<>(pageIndex, pageSize);
        baseMapper.selectPage(page, wrapper);
        return page;
    }


    /**
     * 根据路线id和会员id判断是否已经购买此路线
     * @param routeId
     * @param memberId
     * @return
     */
    @Override
    public Boolean isBuyRoute(String routeId, String memberId) {

        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("route_id", routeId);
        wrapper.eq("member_id", memberId);
        wrapper.eq("status", 1);
        Order order = baseMapper.selectOne(wrapper);
        return order!=null;

    }

    /**
     * 通过路线id和会员id生成订单
     * @param routeId
     * @param memberId
     * @return
     */
    @Override
    public Order createOrder(String routeId, String memberId) {
        OrderRoute route = routeClient.getRouteById(routeId);
        OrderMember member = memberClient.getOrderMemberById(memberId);
        Order order = new Order();
        order.setOrderNo(OrderNoUtil.getOrderNo());
        order.setRouteId(routeId);
        order.setRouteTitle(route.getTitle());
        order.setRouteCover(route.getCover());
        order.setMemberId(memberId);
        order.setNickname(member.getNickname());
        order.setMobile(member.getMobile());
        order.setTotalFee(route.getPrice());
        order.setPayType(1);
        order.setStatus(0);
        baseMapper.insert(order);
        return order;

    }

    /**
     * 通过订单号获取订单信息
     * @param orderNo
     * @return
     */
    @Override
    public Order getOrderByNo(String orderNo) {
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("order_no", orderNo);
        return baseMapper.selectOne(wrapper);
    }

}
